package com.example.primomobile;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.widget.ImageView;

import com.example.primomobile.utils.Utils;

class PecasAddTabuleroDragListener implements OnDragListener {
	private Context context;
	private ImageAdapter imageAdapter;

	public PecasAddTabuleroDragListener(Context context, ImageAdapter imageAdapter) {
		this.context = context;
		this.imageAdapter = imageAdapter;
	}

	@Override
	public boolean onDrag( View view, DragEvent event ) {

		switch ( event.getAction() ) {
			case DragEvent.ACTION_DRAG_STARTED:

				break;
			case DragEvent.ACTION_DRAG_ENTERED:
				break;
			case DragEvent.ACTION_DRAG_EXITED:
				break;
			case DragEvent.ACTION_DROP:

				ImageView peca = (ImageView) event.getLocalState();
				Object tag = peca.getTag();
				if ( tag != null ) {

					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put( Utils.PECA, ( (Map) tag ).get( Utils.PECA ) );
					map.put( Utils.REMOVE, true );
					map.put( Utils.PECA_TROCA, true );

					if ( ( (Map) tag ).containsKey( Utils.PECA_TROCA ) && view.getTag() != null) {
						Drawable aux = ( (ImageView) view ).getDrawable();
						( (Map) view.getTag() ).put( Utils.IMG_ANTERIOR, aux );
						( (ImageView) view ).setImageDrawable( (Drawable) ( (Map) tag ).get( Utils.IMG_ANTERIOR ) );
						peca.setImageDrawable( aux );
						((Map)peca.getTag()).put( Utils.REMOVE, true );

					} else if ( ( (Map) tag ).containsKey( Utils.IMG_ANTERIOR ) ) {
						( (ImageView) view ).setImageDrawable( (Drawable) ( (Map) tag ).get( Utils.IMG_ANTERIOR ) );
					} else {
						( (ImageView) view ).setImageDrawable( peca.getDrawable() );
					}

					view.setTag( map );
				}else{
					Utils.getImageParaTabuleiro( (Integer) ( (HashMap<String, Object>) view.getTag() ).get( Utils.POSICAO ), (ImageView) view );
					view.setVisibility( View.VISIBLE );
				}
				break;
			case DragEvent.ACTION_DRAG_ENDED:
			default:
				break;
		}
		return true;
	}
}